package cn.com.godliu.rabbitmqprovider.consumer;

import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import io.swagger.annotations.Api;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Map;

@Component
@Api(tags = "主题型接收器")

public class TopicManReceiver {
    //@RabbitHandler
    //public void process(Map testMessage) {
    //    System.out.println("TopicManReceiver消费者收到消息  : " + testMessage.toString());
    //}

    @RabbitListener(queues = "man.#")  //留意这里，RabbitListener加入到方法上
    public void process(Message message, Channel channel) throws IOException {
        String msg = new String(message.getBody());

        if (msg.contains("deadletter")) {
            //不消费   模拟进入死信
            channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, false);
        } else if (!msg.contains("reject")) {
            //正常消费
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
            System.out.println("TopicManReceiver消费者收到消息：" + msg);
        } else {
            //消费拒绝   模拟进入死信
            channel.basicReject(message.getMessageProperties().getDeliveryTag(), false);
        }

    }
}
